package com.mindata.blockchain.block.LogMessage;

import com.alibaba.fastjson.JSONObject;
import com.mindata.blockchain.core.model.base.BaseEntity;

import javax.persistence.Entity;
import javax.persistence.Table;
import java.text.SimpleDateFormat;
import java.util.List;
@Entity
@Table(name = "message")
public class LogMessageEntity extends BaseEntity {
    /**
     * 业务id，sql语句中where需要该Id
     */
    private String instructionId;
    /**
     * 区块的序号
     */
    private int number;
    /**
     * 时间戳
     */
    private long timeStamp;

    /**
     * 指令的操作，增删改（1，-1，2）
     */
    private byte operation;
    /**
     * 翻译指令的操作，增删改（1，-1，2）
     */
    private String transOperation;
    /**
     * 旧的内容
     */
    private String oldJson;
    /**
     * 新的内容
     */
    private String json;
    /**
     * 操作的表名
     */
    private String table;

    public String getInstructionId() {
        return instructionId;
    }

    public void setInstructionId(String instructionId) {
        this.instructionId = instructionId;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public long getTimeStamp() {
        return timeStamp;
    }

    public String getTransTimeStamp() {
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = dateformat.format(timeStamp);
        return dateStr;
    }


    public void setTimeStamp(long timeStamp) {
        this.timeStamp = timeStamp;
    }

    public byte getOperation() {
        return operation;
    }

    public void setOperation(byte operation) {
        this.operation = operation;
    }

    public String getTransOperation(){
        switch(operation){
            case -1:transOperation = "删除";break;
            case 1: transOperation = "添加";break;
            case 2: transOperation = "修改";break;
            default:break;
        }
        return transOperation;
    }

    public String getOldJson() {
        return oldJson;
    }

    public void setOldJson(String oldJson) {
        JSONObject oldContent=JSONObject.parseObject(oldJson);
        this.oldJson = oldContent.getString("content");
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        JSONObject newContent = JSONObject.parseObject(json);
        this.json = newContent.getString("content");
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    @Override
    public String toString() {




        return "{要查询的指令ID为：" + instructionId + ";\n"
                + "存储该操作的区块号：" + number + "；\n"
                + "更新信息的时间为：" + getTransTimeStamp() + "；\n"
                + "操作的数据库表：" + table + "；\n"
                + "执行操作的类型：" + getTransOperation() + "；\n"
                + "原来的内容：" + oldJson + "；\n"
                + "更新后的内容：" + json + "};";

    }
}
